﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chaine.DAO;
using MongoDB.Bson;

namespace Chaine.Controllers
{
    public class AdminMemberController : Controller
    {
        //
        // GET: /AdminMember/

        public ActionResult Index()
        {
            var members = AccountManager.GetRegisters();
            return View("~/views/admin/Member/index.cshtml", members);
        }

        //
        // GET: /AdminMember/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminMember/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdminMember/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminMember/Edit/5

        public ActionResult Edit(string id)
        {
            var member = AccountManager.GetRegister(ObjectId.Parse(id));
            return View("~/views/admin/member/edit.cshtml", member);
        }

        //
        // POST: /AdminMember/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection form)
        {
            try
            {
                var member = AccountManager.GetRegister(ObjectId.Parse(id));
                var isAdmin = bool.Parse(form["IsAdmin"]);
                //是否是系统管理员
                if (isAdmin)
                    AccountManager.AddAdminRole(member.Id);
                else
                    AccountManager.RemoveAdminRole(member.Id);
                //是否通过管理员审核
                var isApproved = bool.Parse(form["IsApproved"]);
                member.IsChecked = isApproved;
                //Email是否通过验证
                var emailIsValidated = bool.Parse(form["EmailIsValidated"]);
                member.EmailIsValidated = emailIsValidated;
                AccountManager.UpdateRegister(member);

                TempData["update-successded"] = "";

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminMember/Delete/5

        public ActionResult Delete(string id)
        {
            var member = AccountManager.GetRegister(ObjectId.Parse(id));
            return View("~/views/admin/member/delete.cshtml", member);
        }

        //
        // POST: /AdminMember/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                AccountManager.DeleteMember(ObjectId.Parse(id));
                TempData["delete-success"] = "";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
